
import {
  login
} from "../../utils/api";

import { hasLogin } from "../../utils/localStorage";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    loginStatus:false,
    show: false,
    duration: 300,
    position: 'center',
    round: false,
    overlay: true,
    customStyle: '',
    overlayStyle: '',
    txImg:'',
    userName:'',
    phone:''
  },

  chooseImg(){
    wx.chooseMedia({
      count: 9,
      mediaType: ['image', 'video'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      camera: 'back'
    }).then(res => {
      this.setData({
        txImg: res.tempFiles[0].tempFilePath,
      });
    }).catch(err => {
      console.error('选择媒体文件失败', err);
    });
  },
  toOrderList(){
    wx.navigateTo({
      url: `/pages/orderList/index`,
    })
  },
  backHome: function () {
    wx.reLaunch({
      url: '../index/index',
    })
  },
  back: function () {
    wx.navigateBack({
      delta: 1
    })
  },
  login(e) {
    this.setData({
      show:true
    })
  },
  saveUserInfo(){
    this.loginUser()
    this.setData({
      show:false,
      loginStatus:true
    })
  },

  async loginUser() {
    const res = await login(this.data.userName, this.data.phone)
      wx.setStorage({
        key:"token",
        data:res.token
        })
  },
  bindPhone(e){
    this.setData({
      phone:e.detail.value
    })
  },
  bindName(e){
    this.setData({
      userName:e.detail.value
    })
  },
  changeAddress(){
    wx.navigateTo({
      url: `/pages/address/index`,
    })
  },
 async toYhq(){
    const token = await hasLogin()
    if (!token) {
      wx.showToast({
        title: '请先登录',
        icon: 'error',
        duration: 2000
      })
      return
    }
    wx.navigateTo({
      url: `/pages/yhqList/index`,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})